﻿using IService.Common;
using IService.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class UserController : ControllerBase
    {
        readonly ILogger<UserController> logger;
        readonly IBUserService bUserService;
        readonly ILoginServices loginServices;
        public UserController(ILogger<UserController> _logger, ILoginServices _loginServices, IBUserService _bUserService)
        {
            logger = _logger;
            loginServices = _loginServices;
            bUserService = _bUserService;
        }

        /// <summary>
        /// 用户登录接口：获取Token值
        /// </summary>
        /// <param name="username">账号</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [Description("用户登录接口：获取Token值")]
        public async Task<MessageModel<TokenInfoModel>> UserLogin(string username = "", string password = "", string code = "")
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new MessageModel<TokenInfoModel>()
                {
                    Success = false,
                    Message = "账号或密码不能为空！",
                };
            }
            return await loginServices.UserLogin(username, password, code);
        }


        /// <summary>
        /// 查询所有用户（分页）
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有用户")]
        public async Task<MessageModel<string>> GetAllToPage(int offset = 1, int limit = 15)
        {
            try
            {
                return await bUserService.GetAllToPage(offset, limit);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Description("查询所有用户")]
        public async Task<MessageModel<List<BUser>>> GetAll()
        {
            try
            {
                return await bUserService.GetAll();
            }
            catch (Exception ex)
            {
                return new MessageModel<List<BUser>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按用户名密码查询用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按用户名密码查询用户")]
        public async Task<MessageModel<BUser>> GetByNameAndPwd(string username, string password)
        {
            try
            {
                return await bUserService.GetByNameAndPwd(username, password);
            }
            catch (Exception ex)
            {
                return new MessageModel<BUser>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 按ID查询用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询用户")]
        public async Task<MessageModel<BUser>> GetById(int id)
        {
            try
            {
                return await bUserService.GetById(id);
            }
            catch (Exception ex)
            {
                return new MessageModel<BUser>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("添加用户")]
        public async Task<MessageModel<string>> Post([FromBody] BUser parameter)
        {
            try
            {
                return await bUserService.Post(parameter);
            }
            catch (Exception ex)
            {
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPut]
        [Description("修改用户")]
        public async Task<MessageModel<string>> Put([FromBody] BUser parameter)
        {
            try
            {
                return await bUserService.Put(parameter);
            }
            catch (Exception ex)
            {
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 密码重置
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpPost]
        [Description("密码重置")]
        public async Task<MessageModel<string>> Reset(int id,string password)
        {
            try
            {
                return await bUserService.Reset(id, password);
            }
            catch (Exception ex)
            {
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Description("删除用户")]
        public async Task<MessageModel<bool>> DelById(int id)
        {
            try
            {
                return await bUserService.DelById(id);
            }
            catch (Exception ex)
            {
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

    }
}
